package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.mrobuy.jdvc.dao.CommonDao;
import com.mrobuy.jdvc.dao.ItemDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("commonDao")
public class CommonDaoImpl implements CommonDao {

    private static Logger LOG = LoggerFactory.getLogger(ItemDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Object record) {
        try {
            return SQLExecutor.tempLateInsert(record, jdbcTemplate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int delete(Object record) {
        try {
            return SQLExecutor.delete(record, jdbcTemplate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int update(Object record) {
        return update(record, false);
    }

    @Override
    public int update(Object record, boolean updateNull) {
        try {
            return SQLExecutor.tempLateUpate(record, jdbcTemplate, updateNull);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int execute(String sql, Object... params) throws Exception {
        return SQLExecutor.execute(jdbcTemplate, sql, params);
    }

    @Override
    public <T> List<T> queryList(String sql, Class<T> clz, Object... params) {
        return SQLExecutor.executeQueryBeanList(jdbcTemplate, sql, clz, params);
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
}
